package com.my.chapter18;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.*;

public class practice23 {
    static BitSet isPrintable = new BitSet(127);
    static String encoding =
            System.getProperty("file.encoding");
    static {
        for(int i = 32; i <= 127; i++)
            isPrintable.set(i);
    }
    // Set the position to the last printable character
    public static void setPrintableLimit(CharBuffer cb) {
        cb.rewind();
        while(isPrintable.get(cb.get()));
        cb.limit(cb.position() - 1);
        cb.rewind();
    }

    public static void main(String[] args) {
        System.out.println("Default Encoding is: " + encoding);
        //allocate()分配一个新的精简缓冲区。
        //asCharBuffer()创建此字节缓冲区作为字符缓冲区视图。
        CharBuffer buffer =
                ByteBuffer.allocate(16).asCharBuffer();
        buffer.put("ABCDE" + (char) 0x01 + "FG");
        buffer.rewind();//返回到数据开始部分
        System.out.println(buffer); // Print everything
        setPrintableLimit(buffer);
        System.out.println(buffer); // Print printable
    }
}
